
;
(function($, $$) {
    $.fn.list = function(options) {
        var self = this, args = Array.prototype.slice.call(arguments);
        args.push($$.List);
        var result = $$.createWidgetByJQuery.apply(self, args);
        if (result.jqObj !== undefined && options !== "widget") {
            return self;
        }
        return result;
    };

    $$.List = function(self, options) {
        if (!(this instanceof $$.List)) {
            return new $$.List(self, options);
        }
        var defaults = {

            title : null,

            indexKey : "number",

            titleBarClass : $$.defaultGradientBG,

            titleIcon : null,

            listClass : $$.defaultBorder + " uiCornerAll " + $$.defaultBoxShadow,

            listItemClass : "uiListItem",

            listItemHoverClass : "uiLightBlueGradientBG",

            listItemSelectedClass : "ui30BlueGradientBG",


            click : null,


            listBackIndexArr : [],

            showListItem : null,

            listWidth : 0,

            titleBarHTML : '<div class="uiListTitleBar uiNoSelectText"><span></span><span class="uiListBack uiIcon uiBackIcon">Back</span></div>',

            moreItemHTML : '<span class="uiListMoreBtn uiArrowRightIcon uiSmallIcon"></span>'
        };
        var opts = $.extend(defaults, options);
        $$.List.prototype.superClass.call(this, self, opts);
        this.init();
    }
    $$.inherit($$.List, $$.Widget, {

        init : function() {
            var listObj = this;
            listObj.createWidget();
            initList(listObj.jqObj, listObj.opts);
            return listObj;
        }
    });

    function initList(self, opts) {
        var titleBar = null, title = opts.title || self.attr("title");
        self.addClass("uiList uiWidget " + opts.listClass).children("div:first").addClass("uiListContent");

        if (title) {
            titleBar = $(opts.titleBarHTML).addClass(opts.titleBarClass).children("span:first").html(title).end();
            if (opts.titleIcon) {
                titleBar.prepend($('<span class="uiTitleIcon" />').addClass(opts.titleIcon));
            }
            self.prepend(titleBar);
        }
        self.find("li").each(function() {
            var obj = $(this);
            obj.addClass(opts.listItemClass);
            if (obj.attr(opts.indexKey)) {
                obj.addClass("uiListMore").prepend(opts.moreItemHTML);

            }
        })
        var ulHeight = self.height() - $(">.uiListTitleBar", self).outerHeight(true);
        opts.listWidth = $(">.uiListContent > ul", self).filter(":gt(0)").hide().end().height(ulHeight).width();

        initEvent(self, opts);
        if (opts.disabled) {
        }
    }


    function initEvent(self, opts) {
        $(">.uiListTitleBar >.uiListBack", self).bind("click.uiList", function(e) {
            var number = opts.listBackIndexArr.pop();
            if (opts.listBackIndexArr.length === 0) {
                $(this).fadeOut(opts.defaultAnimateDuration);
            }
            var obj = $(">.uiListContent > ul", self).filter('[' + opts.indexKey + '="' + number + '"]');
            if (obj.length === 0) {
                return;
            }
            var marginLeftValue = $.style(obj[0], "marginLeft");
            obj.css("marginLeft", -opts.listWidth).show().animate({marginLeft : 0}, opts.animateTime, function() {
                if (opts.showListItem != null) {
                    opts.showListItem.hide();
                }
                $.style(this, "marginLeft", marginLeftValue);
            });
        });
        $(">.uiListContent > ul", self).bind("click.uiList", function(e) {
            if (opts.disabled) {
                return false;
            }
            var target = $(e.target);
            if ($.prop !== undefined) {
                if (target.prop("tagName").toUpperCase() != "LI") {
                    target = target.parent();
                }
            }
            else if (target.attr("tagName").toUpperCase() != "LI") {
                target = target.parent();
            }
            if (target.hasClass("uiListMore")) {
                var number = target.attr("number");
                var currentObj = $(this);
                var currentNumber = currentObj.attr("number");
                var obj = currentObj.siblings("[number='" + number + "']").show();
                if (obj.length === 0) {
                    return;
                }
                $(">.uiListTitleBar >.uiListBack", self).fadeIn(opts.defaultAnimateDuration);
                opts.listBackIndexArr.push(currentNumber);
                var marginLeftValue = $.style(this, "marginLeft");
                currentObj.animate({marginLeft : -opts.listWidth}, opts.animateTime, function() {
                    opts.showListItem = obj;
                    $(this).hide();
                    $.style(this, "marginLeft", marginLeftValue);
                });
            }
            else {
                target.removeClass(opts.listItemHoverClass).addClass(opts.listItemSelectedClass)
                    .siblings("." + opts.listItemSelectedClass).toggleClass(opts.listItemClass + " " + opts.listItemSelectedClass);
                if ($.isFunction(opts.click)) {
                    opts.click.call(self[0], target, e);
                }
            }
        })
            .bind("mouseover.uiList", function(e) {
                if (opts.disabled) {
                    return false;
                }
                var target = $(e.target);
                if ($.prop !== undefined) {
                    if (target.prop("tagName").toUpperCase() != "LI") {
                        target = target.parents("li");
                    }
                }
                else if (target.attr("tagName").toUpperCase() != "LI") {
                    target = target.parents("li");
                }
                if (target.hasClass(opts.listItemClass)) {
                    target.removeClass(opts.listItemClass).addClass(opts.listItemHoverClass);
                }
            })
            .bind("mouseout.uiList", function(e) {
                if (opts.disabled) {
                    return false;
                }
                var target = $(e.target);
                if ($.prop !== undefined) {
                    if (target.prop("tagName").toUpperCase() != "LI") {
                        target = target.parents("li");
                    }
                }
                else if (target.attr("tagName").toUpperCase() != "LI") {
                    target = target.parents("li");
                }

                if (target.hasClass(opts.listItemHoverClass)) {
                    target.removeClass(opts.listItemHoverClass).addClass(opts.listItemClass);
                }
            });
    }
})(jQuery, BASE);